<?php
$this->useClass("Action_Community_FeedHandler");
class Action_Community_ShowGallery extends Action_Community_FeedHandler
{

	var $ajax_functions = array(
		'ajax_getSomePosts',
		);

	public function getFeedModel() {
		$this->rh->UseClass("AggregatedModel_Feed_FeedInterface");
		$this->rh->UseClass("AggregatedModel_Feed_ReaderGallery");

		return new AggregatedModel_Feed_ReaderGallery();
	}

	public function ajax_getSomePosts( $page_number ) {

		$this->setParam("page_number", $page_number);

		$this->model->doStuff();

		$view = $this->rh->views->findByName('items:gallery');
		$view->data = $this->acl_assistant->template_data;
		$view->data['items'] = $this->model->getItems();

		$items_node = &$this->rh->ctx->cp->add_node("items");
		$items_node->set_data( $view->getContent() ); // рендеринг итемов

		$this->addPagerNodes( &$this->rh->ctx->cp );

		$this->shutdown();
	}

	protected function init() {
		parent::init();
		$this->m_tdi = $this->ctx->mm->getModel("Tdi", "m");
		$tdi_oids = array();
		foreach ($this->aa->acl_data['tdi_list'] as $tdi) $tdi_oids[] = $tdi['id_object'];
		$this->tdi_types = $this->m_tdi->getSections($tdi_oids);
		$this->rh->useClass('RL_URLGenerator');
	}

	public function getWidgetsConfig ()
	{
		$community_oid = $this->aa->acl_data['comity_oid'];
		$widgets = array();

		$widgets[] = array (
			'name' => 'CreateBtnWidget',
			'params' => array(
				'widget_position' => 'createbtn',
				'widget_view' => 'WG_CreateBtnWidget',
			),
		);

		$widgets[] = array (
			'name' => 'Feed_DoctypesMenuWidget',
			'params' => array(
				'widget_position' => 'tags',
				'widget_view' => 'WG_Feed_DoctypesMenuWidget',
			),
		);

		$widgets[] = array (
			'name' => 'TagsWidget',
			'params' => array(
				'community_oid' => $this->aa->acl_data['comity_oid'],
				'tdi_oid'=> $this->aa->acl_data['tdi_oid'],
				'tag' => $this->getParam('tag'),
				'widget_position' => 'tags',
				'widget_view' => 'WG_Feed_TagsWidget',
			),
		);

		$widgets[] = array (
			'name' => 'RssWidget',
			'params' => array(
				'widget_view' => 'WG_RssWidget',
				'widget_position' => 'tags',
			),
		);

		return $widgets;
	} // end of function getWidgetsConfig

	public function process ()
	{
		$aa = &$this->aa;
		$aa->checkDeletions(array($this, "shutdown") );

		$view = $this->rh->views->findByName('gallery');
		$view->data = $aa->template_data;

		$this->model->doStuff();

		$this->renderFilters($view);

		$view->data['can_create'] = $aa->is_granted(array('item_create', 'item_create_premod'));

		$items = $this->model->getItems();
		if ($items) {
			$view->data['items'] = $items;
		} else {
			$view->data['message'] = "В этой ленте нет сообщений.";
		}


		// данные для постраничной листалки
		$pager = $this->getPagerInfo();
		$view->data['page'] = $pager;
		$view->data['items_remaining'] = $pager['items_remaining'];

		$view->data['__widgets__'] = $this->widgets;


		// генерация урл адресов
		$ug = new RL_URLGenerator($this->rh->router->getLastMatched()); //$this->rh->router->getRule('object_undelete')
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);

		// урл для постраничной листалки без ajax
		if (isset($pager['prev'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['prev'])));
			$view->data["prev_url"] = $ug->generateUrl();
		}

		if (isset($pager['next'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['next'])));
			$view->data["next_url"] = $ug->generateUrl();
		}

		unset($ug);

		// для постраничной листалки ajax handler
		if ( isset($this->params['tdi_url']) ) {
			$ug = new RL_URLGenerator($this->rh->router->getRule('showfeed_tdi_ajax'));
		} else {
			$ug = new RL_URLGenerator($this->rh->router->getRule('showfeed_ajax'));
		}
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);
		$ug->setGet($this->rh->GET);

		$view->data['ajax_url'] = $ug->generateUrl();

		$this->rh->response->setContent( $view );

		// -- touch visitor stat -- {{{
		// visit  tdi
		$this->ctx->mm->getModel("Visitable", "m")->getVisitable('Tdi')->visit();
		//
		// -- touch visitor stat -- }}}

	} // end of function execute


	protected function shutdown() {
		$this->ctx->db->end();
	}

	/*
	private function url_parsing( $get_array ) {
		switch ($get_array['sort']) {
		case 'fname':
			$order_by = 'fname';
			break;
		case 'size':
			$order_by = 'size';
			break;
		case 'date':
			$order_by = 'date';
			break;
		default :
			$order_by = 'date';
			break;
		}

		// --- tags --- {{{
		// контроллер тегов
		// попросили выбирать по тегу?
		$tag = NULL;
		if ( isset($get_array['tag']) ) {
			$tag_name = $get_array['tag'];
			$tag = $this->rh->ctx->store->find( 'select objects where tag.name=:0', array($tag_name))->next();
		}
		// --- tags --- }}}

		$page_number = isset($this->rh->GET['p'])?$this->rh->GET['p']:1;

		return array($tag, $order_by, $page_number);

	}
	*/
}
